package com.example.springmvcdemo.controller;

import com.example.springmvcdemo.model.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/p1")
    public String p1(String name) {
        return "接收到参数,name: "+name;
    }

    @RequestMapping("/p2")
    public String p2(Integer age) {
        return "接收到参数,age: "+age;
    }

    @RequestMapping("/p3")
    public String p3(boolean flg) {
        return "接收到参数,flg: "+flg;
    }

    @RequestMapping("/p4")
    public String p4(String name,Integer age) {
        return "接收到参数,name: "+name+",age: "+age;
    }

    @RequestMapping("/p41")
    public String p41(HttpServletRequest request) {
        String name=request.getParameter("name");
        Integer age=Integer.valueOf(request.getParameter("age"));
        return "接收到参数,name: "+name+",age: "+age;
    }

    @RequestMapping("/p6")
    public String p6(User user) {
        return "user: "+user;
    }

    @RequestMapping("/p7")
    public String p7(@RequestParam(value="Username",required = false) String name) {
        return "name: "+name;
    }

    @RequestMapping("/p8")
    public String p8(String[] arr) {
        return "arr: "+ List.of(arr)+",length: "+arr.length;
    }

    @RequestMapping("/p9")
    public String p8(@RequestParam List<String> list) {
        return "list:"+list;
    }

    @RequestMapping("/p10")
    public String p10(@RequestBody User user) {
        return "user:"+user;
    }

    @RequestMapping("/article/{articleId}")
    public String p11(@PathVariable("articleId") String id) {
        return "articleId:"+id;
    }

    // 当获取url中的多个参数时
    @RequestMapping("/article/{articleId}/{name}")//一个大括号{}表示一个参数
    public String p12(@PathVariable("articleId") String id,@PathVariable(required = false) String name) {
        return "articleId:"+id+",name:"+name;
    }

    @RequestMapping("/p13")
    public String p13(@RequestPart("fileName") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "文件名称:"+file.getOriginalFilename();
    }


}
